﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.Services;
using DreamBuilder.Core.Container;
using System.Text;
using Web.SystemConfig.Services;
using System.Data;

namespace Web.Views.SingleWellManage
{
    /// <summary>
    /// SingleWellTreeHandler 的摘要说明
    /// </summary>
    public class SingleWellTreeHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string json = "[]";
            //获取文档类型
            if (context.Request.Params["archivemenu"] != null)
            {
                json = GetArchiveMenuData();
                //json = TestTreeData();
            }

            /*****************************************************
             * add by guoyin 2014-3-25
             * 根据Category加载CategoryDetail
             ******************************************************/
            if (context.Request.Params["queryCategoryDetail"]!=null)
            {
                if (context.Request.Params["pCategory"] != null)
                {
                    json = GetCategoryDetailByCategory(context.Request.Params["pCategory"]);
                }
            }
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }

        private string TestTreeData()
        {
            return "[{\"id\": \"1\", \"text\": \"10X126-98\",\"pid\": \"0\", \"iconCls\": \"icon-folder\", \"state\": \"open\",\"attributes\":{\"typeCode\": \"W\"}},{\"id\": \"2\", \"text\": \"10X129-99\",\"pid\": \"0\", \"iconCls\": \"icon-folder\", \"state\": \"open\",\"attributes\":{\"typeCode\": \"E\"}}]";
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public string 
            GetArchiveMenuData()
        {
            IWellCatalogService oIWellCatalogService = UContainer.Resolve<IWellCatalogService>();
            WellCatalog.WELLCATALOGDataTable oWellCatalog = oIWellCatalogService.GetAllWellCatalog();
            string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\", \"iconCls\": \"{3}\",  \"state\": \"{4}\",\"attributes\":{{\"typeCode\": \"{5}\"}}}},";
            StringBuilder resultStr = new StringBuilder("[");
            string iconCss = string.Empty;
            string ParentID = string.Empty;
            int i = 1;
            foreach (WellCatalog.WELLCATALOGRow row in oWellCatalog.Rows)
            {
                if (string.IsNullOrEmpty(row.PARENTID.ToString()))
                {
                    ParentID = "0";
                }
                else
                {
                    ParentID = row.PARENTID.ToString();
                }
                iconCss = ParentID == "-1" ? "icon-type" : "icon-folder";
                resultStr.Append(string.Format(jsonFormat, row.WELLCATALOGID, row.CATALOGDESCRIPT, row.PARENTID, iconCss, "open", row.CATELOGSORT));
                i++;
            }
            if (resultStr.Length > 1)
            {
                resultStr = resultStr.Remove(resultStr.Length - 1, 1);
            }
            resultStr.Append("]");
            return resultStr.ToString();
        }

        public string GetCategoryDetailByCategory(string category)
        {
            ICodeTable CodeTableService = UContainer.Resolve<ICodeTable>();
            DataSet dtCategory = CodeTableService.GetCodeTableDataByType(category);
            string jsonFormat = "{{\"text\":\"{0}\",\"value\":\"{1}\"}},";
            string f_Format = "{{\"text\":\"{0}\",\"value\":\"{1}\",\"selected\":true}},";
            StringBuilder jsonReturn=new StringBuilder("[");
            if (dtCategory != null)
            {
                //补充未选择数据
                jsonReturn.AppendFormat(f_Format, "--未选择--", "-1");
                foreach (DataRow row in dtCategory.Tables[0].Rows)
                {
                    jsonReturn.AppendFormat(jsonFormat, row["Code_Desc"].ToString(), row["CodeTableID"].ToString());
                }
            }
            else
            {
                //补充未选择数据
                jsonReturn.AppendFormat(f_Format, "无明细类别", "-2");
            }
            if (jsonReturn.Length > 1)
            {
                jsonReturn.Remove(jsonReturn.Length - 1, 1);
                jsonReturn.Append("]");
            }
            return jsonReturn.ToString();
        }
    }
}